from typing import Any, Dict, Optional, Union, List

from sqlalchemy.orm import Session

from fastapi_common.crud.base import CRUDBase
from fastapi_common.models.menu import Menu
from fastapi_common.schemas.menu import MenuCreate, MenuUpdate
from fastapi_common.models import RoleMenu, Role


class CRUDMenu(CRUDBase[Menu, MenuCreate, MenuUpdate]):

    def list_menu_by_role(self, db: Session, role_id: int) -> List[Menu]:
        return db.query(self.model).join(RoleMenu, RoleMenu.menu_id == self.model.id)\
            .join(Role, RoleMenu.role_id == Role.id).filter(Role.id == role_id)\
            .all()


menu = CRUDMenu(Menu)
